#include <Windows.h>
#include <conio.h>
#include <ctime>
#include <fstream>
#include <iostream>
#include <mmsystem.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <thread>
#include <vector>
#include <windows.h>
// #include "roles.cpp"
#include "Simulated_universe.h"
#include "attack.h"
#include "func.h"
#include "information1.h"
#include "main.h"
#include "music.h"
#include "roles.h"

using namespace std;
int map_xiang();
int print(int x, int y, int mode);
int fighting();
int init();
int keyboard_in();
void _Clearscreen(bool flag);
void hidden();
void show_role_status();
void full_screen();
void MoveWindow2(HWND hwnd, int X, int Y, BOOL bRepaint);
void main1();
void COLOR_PRINT1(const char *s, int front_color, int back_color);
// struct character {
//     string name;
//     int arms_[100];
//     int available = 1;
//     int HP;                     // 血量
//     int ATK;                    // 攻击
//     int DEF;                    // 防御
//     int SPD;                    // 速度
//     int CRIT_rate;              // 暴击
//     int CRIT_DMG;               // 爆伤
//     int Break_Effect;           // 击破特工
//     int Outgoing_Healing_Boost; // 治疗加成
//     int Max_Energy;             // 最大能量
//     int Energy;
//     int Energy_Regeneration_rate; // 能量回复效率
//     int Effect_Hit_Rate;          // 效果命中
//     int Effect_Res;               // 效果抵抗
// };

int achievement1[100], form_yx, form_yy, easter_egg[100], opt[100],
    si_baodi = 0, wu_baodi = 0;
int map_x, map_y;
int role_available[100];
int map_U[1100][1100];
int map_temp[1100][1100];
// struct You {
//     int x, y;
//     int face;
//     string role;
//     character role_status[100];
// };
struct You you;

int map1[1100][1100];
int mapsize = 100, pdchangerole;
char keyin;

int main() {
    you.role_status[2].arms_[2] = 2;
    you.role_status[2].available = 1;
    you.role_status[1].available = 1;
    main1();
    // thread a(start_bgm);
    // thread b(main1);
    // a.join();
    // b.join();
    return 0;
}

void main1() {
    // printt();
    // system("pause");
    init();
    int yx = you.x, yy = you.y;
    // yx=you.x-15;yy=you.y-15;
    // if(you.x<15)yx=1;
    // if(you.y<15)yy=1;
    // cout<<you.x<<" "<<you.y<<" "<<yx<<" "<<yy;
    print(yx, yy, 1);
    while (1) {
        // while(!_kbhit());
        keyin = getch();
        if (keyin >= '1' && keyin <= '6') {
            switch (keyin) {
            case '1':
                you.role = "trailblazer";
                break;
            case '2':
                if (you.role_status[2].available == 1)
                    you.role = "Raiden Mei";
                break;
            case '3':
                if (you.role_status[3].available == 1)
                    you.role = "Firefly";
                break;
            default:
                break;
            }
        }
        if (keyin == 'y') {
            system("cls");
            yue_qian();
        }
        if (keyin == 'c') {
            // system("cls");
            Role_broad();
        }
        if (keyin == 'q') {
            Q_(yx, yy, 1);
            system("cls");
        }
        if (keyin == 'h') {
            help();
        }
        if (keyin == 'g') {
            achievements();
        }
        if (keyin == 'o') {
            options(opt);
        }
        if (keyin == 'f') {
            int axx = you.x, ayy = you.y;
            if (you.face == 1) {
                axx--;
            }
            if (you.face == 2) {
                ayy++;
            }
            if (you.face == 3) {
                axx++;
            }
            if (you.face == 4) {
                ayy--;
            }
            char temp = map1[axx][ayy];
            map1[axx][ayy] = '+';
            _Clearscreen(0);
            print(you.x, you.y, 1);
            if (temp == '!') {
                information2(axx, ayy, easter_egg, opt[1]);
                // system("pause");
                // print(yx,yy,1);
            }
            Sleep(50);
            map1[axx][ayy] = temp;
            _Clearscreen(0);
            print(you.x, you.y, 1);
        }
        if (keyin == 'w') {
            if (you.x > 101) {
                if (map1[you.x - 1][you.y] == 0) {
                    you.x--;
                }
                you.face = 1;
            }
        } else if (keyin == 'a') {
            if (you.y > 101) {
                if (map1[you.x][you.y - 1] == 0)
                    you.y--;
            }
            you.face = 4;
        } else if (keyin == 's') {
            if (you.x < mapsize + 101) {
                if (map1[you.x + 1][you.y] == 0)
                    you.x++;
            }
            you.face = 3;
        } else if (keyin == 'd') {
            if (you.y < mapsize + 101) {
                if (map1[you.x][you.y + 1] == 0)
                    you.y++;
            }
            you.face = 2;
        }
        if (keyin == 'j') {
            attack(yx, yy, 1);
        } else if (keyin == 'k') {
            if (you.face == 1) {
                you.x -= 2;
            }
            if (you.face == 2) {
                you.y += 2;
            }
            if (you.face == 3) {
                you.x += 2;
            }
            if (you.face == 4) {
                you.y -= 2;
            }
        }
        // system("cls");
        _Clearscreen(0);
        print(you.x, you.y, 1);
    }
    // print(30,10);
    // while(1){

    // }
    // cin>>map1[1][1];
    return;
}

int init() {
    const char title[100] = "Star Rail -tk";
    const TCHAR *tcharString = _T("Star Rail -tk");
    SetConsoleTitle(tcharString);
    you.role = "trailblazer";
    system("chcp 65001");
    // 读取地图
    printf("读取地图中 ");
    ifstream infile;
    infile.open("map.txt", ios::in);
    if (!infile.is_open()) {
        cout << "读取文件失败" << endl;
        return 1;
    }
    int line1 = 0;
    char tmp_map1[1100][1100];
    while (infile.getline(tmp_map1[line1], sizeof(tmp_map1[line1]))) {
        // cout<<line1<<map1[line1]<<line1<<endl;
        line1++;
    }
    for (int i = 0; i < line1; i++) {
        for (int j = 0; j < strlen(tmp_map1[i]); j++) {
            if (tmp_map1[i][j] >= '0' && tmp_map1[i][j] <= '9')
                map1[i + 100][j + 100] = int(tmp_map1[i][j]) - 48;
            else
                map1[i + 100][j + 100] = int(tmp_map1[i][j]);
        }
    }
    infile.close();
    printf("地图读取完成 ");
    printf("读取存档中 ");
    // 读取个人信息
    freopen("saver", "r", stdin);
    cin >> you.x >> you.y >> opt[1];
    you.x += 100;
    you.y += 100;
    // cout<<you.x<<you.y;
    // map1[you.x][you.y]='5';
    freopen("CON", "r", stdin);
    printf("存档读取完成");
    printf("正在优化界面中\n");
    hidden(); // 隐藏光标
    // 关闭快速编辑
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hStdin, &mode);
    mode &= ~ENABLE_QUICK_EDIT_MODE;
    mode &= ~ENABLE_INSERT_MODE;
    mode &= ~ENABLE_MOUSE_INPUT;
    SetConsoleMode(hStdin, mode);
    MoveWindow2(GetConsoleWindow(), 0, 0, TRUE);
    char a[100] = "--------------按任意键开始-------------", tmpc;
    int ii = 0;
    while (!kbhit()) {
        // for(int i=0;i<=33;i++)printf("\b");
        _Clearscreen(0);
        tmpc = a[ii];
        a[ii] = '/';
        cout << a;
        a[ii] = tmpc;
        ii++;
        if (ii == 14)
            ii += 12;
        if (ii >= 50)
            ii = 0;
        Sleep(20);
    }
    system("cls");
    // 改变窗口大小
    char cmd[64];
    sprintf(cmd, "mode con cols=%d lines=%d", 150, 100);
    system(cmd);
    // HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    // if (hOut == INVALID_HANDLE_VALUE) return GetLastError();
    // DWORD dwMode = 0;
    // if (!GetConsoleMode(hOut, &dwMode)) return GetLastError();
    // dwMode |= 0x0004;
    // if (!SetConsoleMode(hOut, dwMode)) return GetLastError();
    // wprintf(L"\x1b[38;2;%d;%d;%dmHelloworld", 0, 0, 200);
    // system("pause");
    init_roles();
    return 1;
}

int print(int x, int y, int mode) {
    if (mode == 1) {
        x -= 15;
        y -= 15;
        if (x < 100)
            x = 101;
        if (y < 100)
            y = 101;
        int i = x, j = y;
        cout << you.x - 100 << " " << you.y - 100
             << "上一次按键Last Key: " << keyin << endl;
        printf("    ");
        for (int a = y; a <= y + 30; a++)
            printf("%02d", a);
        cout << endl << "    ";
        for (int a = y; a <= y + 30; a++)
            printf("__");
        printf("\n");
        for (; i <= x + 30; i++) {
            printf("%02d:|", i);
            for (int j = y; j <= y + 30; j++) {
                // printf("%d ",j);
                if (i == you.x && j == you.y) {
                    printf("@ ");
                } else {
                    if (map1[i][j] == 0)
                        printf("%c ", ' ');
                    else if (map1[i][j] == 1)
                        printf("%c ", 'X');
                    else if (map1[i][j] == 119) {
                        COLOR_PRINT1("O", 8, 4);
                        // printf("O ");
                        printf(" ");
                    } else if (map1[i][j] == 210) {
                        COLOR_PRINT1("//", 8, 4);
                        printf("");
                    } else if (map1[i][j] == 211) {
                        COLOR_PRINT1("\\\\", 8, 4);
                        printf("");
                    } else if (map1[i][j] == 212) {
                        COLOR_PRINT1("--", 8, 4);
                        // printf(" ");
                    } else if (map1[i][j] == 213) {
                        COLOR_PRINT1("--", 4, 4);
                        // printf(" ");
                    } else
                        printf("%c ", (char)map1[i][j]);
                    // if(map1[i][j]=='+')
                    //     printf("+ ");
                }
                //
                // if(map1[i][j]==0)
                //     printf("%c ",' ');
            }
            printf("|");
            if (i == x)
                printf("UID:1145141919810");
            if (i == x + 1) {
                printf("1: 开拓者Trailblazer");
                if (you.role == "trailblazer") {
                    printf(" * ");
                    // pdchangerole=0;
                } else {
                    printf("   ");
                }
            }
            if (i == x + 2 && you.role_status[2].available == 1) {
                printf("2: 黄泉Raiden Mei");
                if (you.role == "Raiden Mei") {
                    printf("    * ");
                } else {
                    printf("       ");
                }
            }
            if (i == x + 3 && you.role_status[3].available == 1) {
                printf("3: 流萤Firefly");
                if (you.role == "Firefly") {
                    printf("      * ");
                } else {
                    printf("         ");
                }
            }
            if (i == x + 10) {
                printf(
                    "点击h获得帮助(走不动路可能是因为大写，调成小写就可以了)");
            }
            printf("\n");
        }
        printf("   ");
        for (int a = 1; a <= 32; a++)
            printf("--");
    } else if (mode == 2) {
        printf("%d %d\n", you.x - 100, you.y - 100);
        for (int j = 100; j < y + 100; j++)
            printf("%02d", j - 100);
        printf("\n");
        for (int a = 0; a < y; a++)
            printf("__");
        printf("\n");
        for (int i = 101; i <= x + 100; i++) {
            printf("%02d:|", i - 100);

            for (int j = 100; j < y + 100; j++) {
                if (i == you.x && j == you.y) {
                    printf("@ ");
                } else {
                    if (map_U[i][j] == 0)
                        printf("%c ", ' ');
                    else if (map_U[i][j] == 1)
                        printf("%c ", 'X');
                    else if (map_U[i][j] == 119) {
                        COLOR_PRINT1("O", 8, 4);
                        // printf("O ");
                        printf(" ");
                    } else if (map_U[i][j] == 210) {
                        COLOR_PRINT1("//", 8, 4);
                        printf("");
                    } else if (map_U[i][j] == 211) {
                        COLOR_PRINT1("\\\\", 8, 4);
                        printf("");
                    } else if (map_U[i][j] == 212) {
                        COLOR_PRINT1("--", 8, 4);
                        // printf(" ");
                    } else if (map_U[i][j] == 213) {
                        COLOR_PRINT1("--", 4, 4);
                        // printf(" ");
                    } else
                        printf("%c ", (char)map_U[i][j]);
                    // if(map1[i][j]=='+')
                    //     printf("+ ");
                }
                //
                // if(map1[i][j]==0)
                //     printf("%c ",' ');
            }
            printf("|");
            if (i == 101)
                printf("UID:1145141919810");
            if (i == 102) {
                printf("1: 开拓者Trailblazer");
                if (you.role == "trailblazer") {
                    printf(" * ");
                    // pdchangerole=0;
                } else {
                    printf("   ");
                }
            }
            if (i == 103 && you.role_status[2].available == 1) {
                printf("2: 黄泉Raiden Mei");
                if (you.role == "Raiden Mei") {
                    printf("    * ");
                } else {
                    printf("       ");
                }
            }
            if (i == 104 && you.role_status[3].available == 1) {
                printf("3: 流萤Firefly");
                if (you.role == "Firefly") {
                    printf("      * ");
                } else {
                    printf("         ");
                }
            }
            if (i == 111) {
                printf(
                    "点击h获得帮助(走不动路可能是因为大写，调成小写就可以了)");
            }
            printf("\n");
        }
        printf("   ");
        for (int a = 0; a < y; a++)
            printf("--");
    }
    return 1;
}

void _Clearscreen(bool flag) { // 清空屏幕
    if (flag == 1) {           // 双缓冲
        HANDLE hStdOut;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD count;
        DWORD cellCount;
        COORD homeCoords = {0, 0};
        hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        if (hStdOut == INVALID_HANDLE_VALUE)
            return;
        if (!GetConsoleScreenBufferInfo(hStdOut, &csbi))
            return;
        cellCount = csbi.dwSize.X * csbi.dwSize.Y;
        if (!FillConsoleOutputCharacter(hStdOut, (TCHAR)' ', cellCount,
                                        homeCoords, &count))
            return;
        if (!FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, cellCount,
                                        homeCoords, &count))
            return;
        SetConsoleCursorPosition(hStdOut, homeCoords);
    } else if (flag == 0) { // 单缓冲
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD coordScreen = {0, 0};
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
}

void hidden() {
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut, &cci);
    cci.bVisible = 0;
    SetConsoleCursorInfo(hOut, &cci);
}

/*******************************************************************************
 * 获取控制台当前字体大小
 ******************************************************************************/
RECT GetWindowRect2(HWND hwnd) { // 换个名称即可
    RECT rt;
    GetWindowRect(hwnd, &rt); // 这是Windows原版函数
    return rt;
}

LONG GetWindowWidth(HWND hwnd) {
    RECT rect = GetWindowRect2(hwnd);
    return (rect.right - rect.left);
}
LONG GetWindowHeight(HWND hwnd) {
    RECT rect = GetWindowRect2(hwnd);
    return (rect.bottom - rect.top);
}
void MoveWindow2(HWND hwnd, int X, int Y, BOOL bRepaint) {
    LONG w, h;
    w = GetWindowWidth(hwnd);
    h = GetWindowHeight(hwnd);
    MoveWindow(hwnd, X, Y, w, h, bRepaint); // 原版函数
}
void COLOR_PRINT1(const char *s, int front_color, int back_color) {
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | back_color * 16 |
                                        FOREGROUND_INTENSITY | front_color);
    printf(s);
    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
/*
0 = 黑色 8 = 灰色
1 = 蓝色 9 = 淡蓝色
2 = 绿色 10 = 淡绿色
3 = 浅绿色 11 = 淡浅绿色
4 = 红色 12 = 淡红色
5 = 紫色 13 = 淡紫色
6 = 黄色 14 = 淡黄色
7 = 白色 15 = 亮白色
*/